package com.aps.apspda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.aps.apspda.R;
import com.aps.apspda.adapter.InputDataListAdapter;
import com.aps.apspda.base.BaseActivity;
import com.aps.apspda.callback.DialogEntityCallBack;
import com.aps.apspda.callback.EntityCallBack;
import com.aps.apspda.entity.BaseEntity;
import com.aps.apspda.entity.FinishLotEntity;
import com.aps.apspda.entity.InputDataEntity;
import com.aps.apspda.myview.TitleView;
import com.aps.apspda.utils.AntiShake;
import com.aps.apspda.utils.NetUtils;
import com.aps.apspda.utils.StaticMembers;
import com.aps.apspda.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class LoggingTCMActivity extends BaseActivity {


    @BindView(R.id.topView)
    TitleView topView;
    @BindView(R.id.tvDataName)
    TextView tvDataName;
    @BindView(R.id.rvDataList)
    RecyclerView rvDataList;

    private List<InputDataEntity.InputObject> inputList = new ArrayList<>();
    private String subeqpID;
    private String eqpID;
    private String datacName;
    private InputDataListAdapter inputDataListAdapter;
    private List<InputDataEntity.InputObject> submitInputList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_tcm;
    }

    @Override
    protected void initView() {
        tvDataName.setText(getIntent().getStringExtra("dataName"));
        datacName = getIntent().getStringExtra("datacName");
        subeqpID = getIntent().getStringExtra("subeqpID");
        eqpID = getIntent().getStringExtra("eqpID");
//        topView.setTitle("记录数据(" + StaticMembers.CUR_USER.getUSERNAME() + ")");
        rvDataList.setLayoutManager(new LinearLayoutManager(LoggingTCMActivity.this));
        rvDataList.setItemAnimator(new DefaultItemAnimator());
        rvDataList.addItemDecoration(new DividerItemDecoration(LoggingTCMActivity.this, 1));
        topView.setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoggingTCMActivity.this.finish();
            }
        });
    }

    @Override
    protected void initData() {
        setData();
    }

    private void setData() {
        InputDataEntity inputList = (InputDataEntity) getIntent().getSerializableExtra("inputList");



        inputDataListAdapter = new InputDataListAdapter(inputList.getINPUTDATA());
        inputDataListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        inputDataListAdapter.isFirstOnly(true);
        inputDataListAdapter.setIsSetValue(true);
        rvDataList.setAdapter(inputDataListAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    @OnClick({R.id.tvLogging})
    public void onClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            ToastUtils.showFreeToast("请勿重复点击",
                    LoggingTCMActivity.this, false, Toast.LENGTH_SHORT);
            return;
        }
        switch (view.getId()) {
            case R.id.tvLogging:
                //提交数据
                boolean isInputOk = true;
                if (inputDataListAdapter != null) {
                    submitInputList = inputDataListAdapter.getData();
                    for (InputDataEntity.InputObject inputObject : submitInputList) {
                        inputObject.setEQPID(subeqpID);
                        if (inputObject.getISNULL().equalsIgnoreCase("Y") &&
                                inputObject.getDATAVALUE().length() == 0) {
                            ToastUtils.showFreeToast("请填写带*栏数据", LoggingTCMActivity.this, false, Toast.LENGTH_SHORT);
                            isInputOk = false;
                        } else {
                            if (inputObject.getDATAVALUE().contains("NG")) {
                                String[] strs = inputObject.getDATAVALUE().split(";");
                                if (strs.length <= 1) {
                                    isInputOk = false;
                                    ToastUtils.showFreeToast("请给NG项填写原因", LoggingTCMActivity.this, false, Toast.LENGTH_SHORT);
                                }
                            }
                        }
                        if (!isInputOk) {
                            break;
                        }
                    }
                }
                if (isInputOk) {
                    requestInputData();
                }
                break;
        }
    }

    private void requestInputData() {
        EntityCallBack<BaseEntity<FinishLotEntity>> callBack = new DialogEntityCallBack<BaseEntity<FinishLotEntity>>
                (new TypeToken<BaseEntity<FinishLotEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<FinishLotEntity>> response) {
                if (response.body().isSuccess(LoggingTCMActivity.this)) {
                    loadSuccess(response.body().getMessage());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            LoggingTCMListActivity.instance.finish();
                            LoggingTCMActivity.this.finish();
                        }
                    }, 1000);


                } else {
                    loadFail(response.body().getMessage());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<FinishLotEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "SubmitInputData");
            }
        };

        Map<String, String> map = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("DATACNNAME", datacName);
            jsonObject2.put("DATANAME", tvDataName.getText());
            if (submitInputList.size() == 0) {
                jsonObject2.put("INPUTDATAS", new JSONObject().put("INPUTDATA", ""));
            } else {
                JSONArray jsonArray = new JSONArray(new Gson().toJson(submitInputList));
                jsonObject2.put("INPUTDATAS", new JSONObject().put("INPUTDATA", jsonArray));
            }
            jsonObject.put("INPUTDATA", jsonObject2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("InputDataList", jsonObject.toString());
        map.put("OPID", StaticMembers.CUR_USER.getUSERNAME());
        map.put("EqpId", subeqpID);
        map.put("InputDataType", "LOT");
        NetUtils.requestNet(this, "/SubmitInputData", map, callBack);
    }
}
